#include "stdafx.h"
#include "MyVariableSizeFileReader.h"

MyVariableSizeFileReader::MyVariableSizeFileReader()
{

}

MyVariableSizeFileReader::~MyVariableSizeFileReader()
{

}

_BIN_FILE_RESULT MyVariableSizeFileReader::GetRecordFromOffset(FileReader* pfr, u64 offset, void* pRecord, u32* pSize)
{
	char* p = static_cast<char* >(pRecord);
	u64 fileSize = pfr->GetFileSize();
	u32 len, size;

	if (offset == fileSize) return BIN_FILE_END;
	if (offset > fileSize)
	{
		CZ_LOG_ERRORA("");
		return BIN_FILE_ERROR;
	}
	
	size = 0;

	if (!pfr->Getu32(offset + size, len))
	{
		CZ_LOG_ERRORA("");
		return BIN_FILE_ERROR;
	}
	size += 4;
	
	if (p != NULL)
	{
		if (!pfr->GetData(offset + size, reinterpret_cast<u8* >(p), len))
		{
			CZ_LOG_ERRORA("");
			return BIN_FILE_ERROR;
		}
	}
	size += len;
	
	if (pSize != NULL) *pSize = size;

	return BIN_FILE_SUCCESS;
}
